package com.xinghou.platform.dao.main.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import com.xinghou.platform.dao.bases.JdbcTemplate;
import com.xinghou.platform.dao.bases.database.CustomerContextHolder;
import com.xinghou.platform.dao.main.PhotoDAO;
import com.xinghou.platform.model.main.Photo;
/**
 * @author liuwei.wen
 *
 */
@Service
public class PhotoDAOImpl {/*
	@Resource
	private JdbcTemplate jdbcTemplate;
	@Override
	public Photo getPhotoById(Integer Id) throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		String sql = "select Id,User_id,Album_id,NAME,Photo_uri,STATUS,Praise_count,Create_time,Update_time from photo where Id = ?";
		Photo photo = jdbcTemplate.queryForObject(sql, new Object[] { Id }, new PhotoMapper());

		CustomerContextHolder.clearCustomerType();
		return photo;
	}

	@Override
	public int updatePhoto(Photo photo) throws DataAccessException {
		StringBuffer strSQL = new StringBuffer("UPDATE photo set User_id = ?, Album_id = ?, NAME = ?, Photo_uri = ?, STATUS = ?,")
		.append("Praise_count = ?, Create_time = ?, Update_time = ? where Id=?");

       Object[] params = {photo.getUserId(), photo.getAlbumId(), photo.getName(), photo.getPhotoUri(), photo.getStatus(),
    		   photo.getPraiseCount(), photo.getCreateTime(), photo.getUpdateTime(), photo.getId()
     };

    CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_WRITE);
    int ret = jdbcTemplate.update(strSQL.toString(),params);
    CustomerContextHolder.clearCustomerType();
      return ret;
	}

	@Override
	public int savePhoto(Photo photo) throws DataAccessException {
		StringBuffer strSQL = new StringBuffer("INSERT INTO photo(User_id,Album_id,NAME,Photo_uri,STATUS,Praise_count,Create_time,Update_time)values(?,?,?,?,?,?,?,?)");

       Object[] params = {photo.getUserId(), photo.getAlbumId(), photo.getName(), photo.getPhotoUri(), photo.getStatus(),
    	 photo.getPraiseCount(), photo.getCreateTime(), photo.getUpdateTime()
    };

     CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_WRITE);
     int ret=jdbcTemplate.save(strSQL.toString(),params);
     CustomerContextHolder.clearCustomerType();
     return ret;
	}

	public Photo Photo(Integer userid) throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
       String sql="select Id,User_id,Album_id,NAME,Photo_uri,STATUS,Praise_count,Create_time,Update_time from photo where User_id=?";
        Photo Photo=(Photo) jdbcTemplate.queryForObject(sql, new Object[] {userid}, new PhotoMapper());
		return Photo;
	}

	@Override
	public Photo PhotoByuserId(Integer frompage, Integer userId,Integer endpage)
			throws DataAccessException {
		   CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
	       String sql="select Id,User_id,Album_id,NAME,Photo_uri,STATUS,Praise_count,Create_time,Update_time from photo where User_id=? and Id>? ORDER BY Id DESC  LIMIT ?";
	       Photo photo=jdbcTemplate.queryForObject(sql, new Object[] {userId,frompage,endpage}, new PhotoMapper());
		   return photo;
	}

	private static final class PhotoMapper implements RowMapper<Photo> {
		public Photo mapRow(ResultSet rs, int rowNum) throws SQLException {
			Photo photo = new Photo();
			photo.setAlbumId(rs.getInt("Album_id"));
			photo.setCreateTime(rs.getTimestamp("Create_time"));
			photo.setId(rs.getInt("Id"));
			photo.setName(rs.getString("NAME"));
			photo.setPhotoUri(rs.getString("Photo_uri"));
			photo.setPraiseCount(rs.getInt("Praise_count"));
			photo.setStatus(rs.getShort("STATUS"));
			photo.setUpdateTime(rs.getTimestamp("Update_time"));
			photo.setUserId(rs.getInt("User_id"));
			return photo;
		}
	}

	public List<Photo> getPhotos(Integer userid,Integer fromno,Integer limit) throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
       String sql="select Id,User_id,Album_id,NAME,Photo_uri,STATUS,Praise_count,Create_time,Update_time from photo where User_id=? AND Id<? ORDER BY Create_time DESC LIMIT ?";
       List<Photo> list = jdbcTemplate.query(sql, new Object[] {userid,fromno,limit}, new PhotoMapper());
       CustomerContextHolder.clearCustomerType();
		return list;
	}*/

}
